package com.weavernorth.humanresource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import sun.misc.BASE64Encoder;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.Iterator;

/**
 * Created by guo_zy on 2020/3/5.
 * 调动成本中心推送
 */
public class personnelTransfercostcenter implements Action {
    public static Log log = LogFactory.getLog(personnelEntrycostcenter.class.getName());
    @Override
    public String execute(RequestInfo requestInfo) {
        String fhz = "";
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet rsRsfw=new RecordSet();
        String requestid = requestInfo.getRequestid();//请求ID
        //String workflowid = requestInfo.getWorkflowid();//流程ID
        String tablename = requestInfo.getRequestManager().getBillTableName();//表单名称
        String hth="";//员工编号
        String rzrq="";//入职日期
        String  cbzx="";//成本中心
        String  cbzxbfb="";//成本中心百分比
        String zbid="";//主表ID
        String strMessage="";
        //人事范围
        String  strRsfw="";

        String sql = "select ygbh,rzsj,rsfwh,cbzx from " + tablename + " where requestid =" + requestid;
        rs.execute(sql);
        if (rs.getCounts() != 1) {
            return FAILURE_AND_CONTINUE;
        }
        while (rs.next()) {
            hth = rs.getString("ygbh");
            rzrq = rs.getString("rzsj");
            strRsfw = rs.getString("rsfwh");
            cbzx = rs.getString("cbzx");

        }
        //查询人事范围
        String strRsfwDm="";
        String strRsfwSql="select rsfwdm from uf_rsfw where id = ?";
        rsRsfw.executeQuery(strRsfwSql,strRsfw);
        if(rsRsfw.next()){
            strRsfwDm=Util.null2String(rsRsfw.getString("rsfwdm"));
        }

        sql = "select cbzx from uf_cbzxxx where   id = "+cbzx;
        rs.execute(sql);
        while(rs.next()){
            cbzx = rs.getString("cbzx");//成本中心
        }

        //拼接xml
        StringBuffer xml = new StringBuffer("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">");
        xml.append("<soapenv:Header/>");
        xml.append("<soapenv:Body>");
        xml.append("<urn:Zhri081>");
        xml.append("<Return>");
        xml.append("<item>");
        xml.append("<Type></Type>");
        xml.append("<Id></Id>");
        xml.append("<Number></Number>");
        xml.append("<Message></Message>");
        xml.append("<LogNo></LogNo>");
        xml.append("<LogMsgNo></LogMsgNo>");
        xml.append("<MessageV1></MessageV1>");
        xml.append("<MessageV2></MessageV2>");
        xml.append("<MessageV3></MessageV3>");
        xml.append("<MessageV4></MessageV4>");
        xml.append("<Parameter></Parameter>");
        xml.append("<Row></Row>");
        xml.append("<Field></Field>");
        xml.append("<System></System>");
        xml.append("</item>");
        xml.append("</Return>");
        xml.append("<T_0027>");
        xml.append("<item>");


        xml.append("<Pernr>"+hth+"</Pernr>");
        xml.append("<Begda>"+rzrq+"</Begda>");
        xml.append("<Zkbu01>"+strRsfwDm+"</Zkbu01>");
        xml.append("<Zkst01>"+cbzx+"</Zkst01>");
        xml.append("<Zkpr01>100</Zkpr01>");
        xml.append("</item>");
        xml.append("</T_0027>");
        xml.append("</urn:Zhri081>");
        xml.append("</soapenv:Body>");
        xml.append("</soapenv:Envelope>");
        log.info("成本中心推送" + xml.toString());
        try{
            SalarRangeUtil salarRangeUtil=new SalarRangeUtil();
            String dxUrl = bb.getPropValue("personnel-erdos", "rztscbzxurl");//获取配置文件信息
            String responseXml = salarRangeUtil.sendPost(dxUrl, xml.toString(), "utf-8");
            log.info("成本中心返回值:" + responseXml);//打印日志
            Document doc = null;//解析返回值开始
            try {
                doc = DocumentHelper.parseText(responseXml);
                Iterator<Element> its2= doc.selectNodes("//Type").iterator();
                while (its2.hasNext()){
                    Element element=its2.next();
                    fhz = element.getText();
                    if(!"".equals(fhz)) {
                        break;
                    }
                }


                //返回值失败时,则提示失败
                if ("E".equals(fhz)) {
                    Iterator<Element> its3 = doc.selectNodes("//Message").iterator();
                    while (its3.hasNext()) {
                        Element element2 = its3.next();
                        strMessage = element2.getText();
                        if (!"".equals(strMessage)) {
                            break;
                        }
                    }

                }
            } catch (DocumentException e) {
                e.printStackTrace();
            }//解析结束
        }catch (Exception e){
            e.printStackTrace();
        }
        if ("S".equals(fhz)) {
            return SUCCESS;
        } else {
            if (!"".equals(strMessage)) {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
            }

            return FAILURE_AND_CONTINUE;
        }
    }


}
